package com.xiaoyao.novel.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.xiaoyao.novel.pojo.Chapter;

public class ChapterDao extends BaseDao<Chapter> {

	public int getNextChapter(Map<String, Integer> map) throws SQLException {
		Object object = session.queryForObject(PREFIX + "nextChapter", map);
		if (object == null) {
			return 0;
		} else {
			return Integer.parseInt("" + object);
		}
	}
	public int getPrevChapter(Map<String, Integer> map) throws SQLException {
		Object object = session.queryForObject(PREFIX + "prevChapter", map);
		if (object == null) {
			return 0;
		} else {
			return Integer.parseInt("" + object);
		}
	}
	public List<Integer> getPrevNextChapter(Map<String, Integer> map) throws SQLException {
		List<Object> list = session.queryForList(PREFIX + "prevNextChapter", map);
		List<Integer> idList =new ArrayList<Integer>();
		if (list!=null && list.size()==2) {
			for(Object object:list){
				if (object!=null) {
					idList.add(Integer.parseInt(object+"")) ;
				}else {
					idList.add(0);
				} 
			} 
		}else {
			idList.add(0); 
			idList.add(0); 
		} 
		return idList ; 
	}
	public void deleteBook(int bookId) throws SQLException {
		session.delete(PREFIX + "deleteBook", bookId);
	}
	public static void main(String[] args) throws SQLException {
		ChapterDao chapterDao = new ChapterDao();

		Map<String, Integer> map = new HashMap<String, Integer>();
		map.put("bookId", 355);
		map.put("cid", 152210);
		
		List<Integer>  prevNextList = 	chapterDao.getPrevNextChapter(map);
		System.out.println(prevNextList);
		
	}
}
